package com.ruoyi.mng.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mng.domain.HzMerBalanceLog;
import com.ruoyi.mng.service.IHzMerBalanceLogService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzMerBalanceMapper;
import com.ruoyi.mng.domain.HzMerBalance;
import com.ruoyi.mng.service.IHzMerBalanceService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商户额度Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-28
 */
@Service
public class HzMerBalanceServiceImpl implements IHzMerBalanceService {
    @Autowired
    private HzMerBalanceMapper hzMerBalanceMapper;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;
    @Resource
    private IHzMerBalanceLogService hzMerBalanceLogService;

    /**
     * 查询商户额度
     *
     * @param balanceId 商户额度主键
     * @return 商户额度
     */
    @Override
    public HzMerBalance selectHzMerBalanceByBalanceId(Long balanceId) {
        HzMerBalance hzMerBalance = hzMerBalanceMapper.selectHzMerBalanceByBalanceId(balanceId);
        hzMerBalance.setExtend_merchantName(hzMerMerchantService.selectHzMerMerchantByMerchantId(hzMerBalance.getMerchantId()).getMerchantShortName());
        return hzMerBalance;
    }

    /**
     * 查询商户额度列表
     *
     * @param hzMerBalance 商户额度
     * @return 商户额度
     */
    @Override
    public List<HzMerBalance> selectHzMerBalanceList(HzMerBalance hzMerBalance) {
        List<HzMerBalance> hzMerBalanceList = hzMerBalanceMapper.selectHzMerBalanceList(hzMerBalance);
        if (hzMerBalanceList != null && hzMerBalanceList.size() > 0) {
            hzMerBalanceList.forEach(hzMerBalancePO -> {
                hzMerBalancePO.setExtend_merchantName(hzMerMerchantService.selectHzMerMerchantByMerchantId(hzMerBalancePO.getMerchantId()).getMerchantShortName());
            });
        }
        return hzMerBalanceList;
    }

    /**
     * 新增商户额度
     *
     * @param hzMerBalance 商户额度
     * @return 结果
     */
    @Override
    public int insertHzMerBalance(HzMerBalance hzMerBalance) {
        hzMerBalance.setCreateTime(DateUtils.getNowDate());
        hzMerBalance.setUpdateTime(DateUtils.getNowDate());
        return hzMerBalanceMapper.insertHzMerBalance(hzMerBalance);
    }

    /**
     * 修改商户额度
     *
     * @param hzMerBalance 商户额度
     * @return 结果
     */
    @Override
    public int updateHzMerBalance(HzMerBalance hzMerBalance) {
        hzMerBalance.setUpdateTime(DateUtils.getNowDate());
        return hzMerBalanceMapper.updateHzMerBalance(hzMerBalance);
    }

    /**
     * 批量删除商户额度
     *
     * @param balanceIds 需要删除的商户额度主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBalanceByBalanceIds(String balanceIds) {
        return hzMerBalanceMapper.deleteHzMerBalanceByBalanceIds(Convert.toStrArray(balanceIds));
    }

    /**
     * 删除商户额度信息
     *
     * @param balanceId 商户额度主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBalanceByBalanceId(Long balanceId) {
        return hzMerBalanceMapper.deleteHzMerBalanceByBalanceId(balanceId);
    }

    /**
     * description :: 商户入账
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addBalance(HzMerBalance hzMerBalance) {
        //操作金额
        String extend_amount = hzMerBalance.getExtend_amount();
        //更新余额
        hzMerBalanceMapper.addBalance(hzMerBalance);
        //新增日志
        HzMerBalanceLog hzMerBalanceLog = new HzMerBalanceLog();
        hzMerBalanceLog.setBalanceId(hzMerBalance.getBalanceId());
        hzMerBalanceLog.setDealType(1L);
        hzMerBalanceLog.setAmount(new BigDecimal(extend_amount).multiply(new BigDecimal(100)));
        hzMerBalanceLog.setRemark("人工入账：" + extend_amount + "元");
        hzMerBalanceLog.setDelFlag("0");
        hzMerBalanceLog.setCreateTime(new Date());
        hzMerBalanceLog.setUpdateTime(new Date());
        return hzMerBalanceLogService.insertHzMerBalanceLog(hzMerBalanceLog);
    }
}
